package com.vogue.supplymall.account.domain.provider;

import com.vogue.ssm.database.provider.BaseSqlProvider;
import com.vogue.supplymall.common.domain.dto.admin.OrganSearchInfo;
import org.apache.commons.lang3.StringUtils;

/**
 * The type Cart provider.
 *
 * @aouthor: zhangliang
 * @date: Create in 2018-05-02
 */
public class VaildOrganSqlProvider extends BaseSqlProvider {
    /**
     * Find string.
     *
     * @param organSearchInfo the organ search info
     * @return the string
     */
    public String find(OrganSearchInfo organSearchInfo) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT ")
                .append(" 	bc.id, ")
                .append(" 	br.id rid, ")
                .append(" 	bc.syscode, ")
                .append(" 	bu.tel, ")
                .append(" 	bc.channel, ")
                .append(" 	bc.tpsystem, ")
                .append(" 	bc.besellerdate, ")
                .append(" 	br.registname ");
        sql.append(" FROM ")
                .append(" 	base_code bc ")
                .append(" INNER JOIN rel_user_code ruc ON ruc.syscodeid = bc.id ")
                .append(" INNER JOIN base_user bu ON ruc.userid = bu.id ")
                .append(" INNER JOIN base_registinfo br ON bc.id = br.syscodeid ");
        sql.append(" WHERE ")
                .append(" bc.disabledflag = 0 ")
                .append(" AND bu.disabledflag = 0 ")
                // 注册但未开通
                .append(" AND br.disabledflag = 1 ")
                .append(" AND bc.sellerflag = 1 ");
        if (StringUtils.isNotEmpty(organSearchInfo.getOrgname())) {
            sql.append(" AND br.registname LIKE CONCAT('%',#{orgname},'%') ");
        }
        if (null != organSearchInfo.getSyscode()) {
            sql.append(" AND bc.syscode LIKE CONCAT('%',#{syscode},'%') ");
        }
        if (StringUtils.isNotEmpty(organSearchInfo.getTel())) {
            sql.append(" AND bu.tel LIKE CONCAT('%',#{tel},'%') ");
        }
        if (StringUtils.isNotEmpty(organSearchInfo.getStartDate())) {
            sql.append(" AND ( bc.besellerdate >= #{startDate} ");
        }
        if (StringUtils.isNotEmpty(organSearchInfo.getEndDate())) {
            sql.append("  AND bc.besellerdate <= #{endDate} ) ");
        }
        sql.append(" GROUP BY bc.id ");
        sql.append(" ORDER BY registname desc,besellerdate ");
        sql.append(" LIMIT #{start},#{length}");
        return sql.toString();
    }


    /**
     * Find string.
     *
     * @param organSearchInfo the organ search info
     * @return the string
     */
    public String findCount(OrganSearchInfo organSearchInfo) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT ")
                .append(" count(DISTINCT bc.id) ");
        sql.append(" FROM ")
                .append(" 	base_code bc ")
                .append(" INNER JOIN rel_user_code ruc ON ruc.syscodeid = bc.id ")
                .append(" INNER JOIN base_user bu ON ruc.userid = bu.id ")
                .append(" INNER JOIN base_registinfo br ON bc.id = br.syscodeid ");
        sql.append(" WHERE ")
                .append(" bc.disabledflag = 0 ")
                .append(" AND bu.disabledflag = 0 ")
                // 注册但未开通
                .append(" AND br.disabledflag = 1 ")
                .append(" AND bc.sellerflag = 1 ");
        if (StringUtils.isNotEmpty(organSearchInfo.getOrgname())) {
            sql.append(" AND br.registname LIKE CONCAT('%',#{orgname},'%') ");
        }
        if (null != organSearchInfo.getSyscode()) {
            sql.append(" AND bc.syscode LIKE CONCAT('%',#{syscode},'%') ");
        }
        if (StringUtils.isNotEmpty(organSearchInfo.getTel())) {
            sql.append(" AND bu.tel LIKE CONCAT('%',#{tel},'%') ");
        }
        if (StringUtils.isNotEmpty(organSearchInfo.getStartDate())) {
            sql.append(" AND ( bc.besellerdate >= #{startDate} ");
        }
        if (StringUtils.isNotEmpty(organSearchInfo.getEndDate())) {
            sql.append("  AND bc.besellerdate <= #{endDate} ) ");
        }
        return sql.toString();
    }
}
